package com.alvin.demo.ui;

import com.alvin.demo.bean.RequestEntity;
import com.alvin.demo.utils.Tcp_Utils_Nio;

import javax.swing.*;
import java.awt.*;

public class MainWindow extends JFrame {
    public MainWindow() {
        Tcp_Utils_Nio tcp = new Tcp_Utils_Nio();
        RequestEntity requestEntity = new RequestEntity();
        setTitle("ESD测试工具");

        setSize(580,230);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel idLabel = new JLabel("终端ID:");

        JTextField idTextField = new JTextField(8);

        JLabel commandLabel = new JLabel("操作指令:");

        JTextField commandTextField = new JTextField(30);

        JLabel wgLabel = new JLabel("网关IP:");

        JComboBox<String> wgComboBox = new JComboBox<>();
        wgComboBox.addItem("192.168.4.102");
        wgComboBox.addItem("192.168.4.101");
        wgComboBox.addItem("192.168.4.103");
        wgComboBox.addItem("192.168.4.104");

        JLabel timeOutLabel = new JLabel("延迟读取时间:");
        JComboBox<String> timeOutCom = new JComboBox<>();
        timeOutCom.addItem("300ms");
        timeOutCom.addItem("500ms");
        timeOutCom.addItem("800ms");
        timeOutCom.addItem("1000ms");
        timeOutCom.addItem("3000ms");

        JLabel crcTypeLable = new JLabel("CRC存储方式:");
        JComboBox<String> crcTypeOutCom = new JComboBox<>();
        crcTypeOutCom.addItem("小端");
        crcTypeOutCom.addItem("大端");

        JLabel totalLabel = new JLabel("完整操作指令:");
        JTextField totalTextField = new JTextField(40);
        totalTextField.setEditable(false);

        JButton button = new JButton("提交");
        JButton button1 = new JButton("清空");

        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

        add(idLabel);
        add(idTextField);

        add(commandLabel);
        add(commandTextField);

        add(wgLabel);
        add(wgComboBox);

        add(timeOutLabel);
        add(timeOutCom);

        add(crcTypeLable);
        add(crcTypeOutCom);
        add(button);

        add(totalLabel);
        add(totalTextField);

        JLabel resultLabel = new JLabel("返回的结果:");
        JTextArea resultTextArea = new JTextArea(4,36);
        resultTextArea.setEditable(false);
        resultTextArea.setLineWrap(true);
        add(resultLabel) ;
        add(resultTextArea);
        add(button1);
        button.addActionListener(e -> {
            resultTextArea.setText("");
            String id = idTextField.getText().trim().replace(" ", "");
            String command = commandTextField.getText().trim().replace(" ", "");
            String s =(String) timeOutCom.getSelectedItem();
            String crcType = (String) crcTypeOutCom.getSelectedItem();
            int timeOut = 300;
            if (s != null) {
                timeOut = Integer.parseInt(s.substring(0 , s.length()-2));
            }
            String s1 =(String) wgComboBox.getSelectedItem();
            String serverIp = "192.168.4.101" ;
            if (s1 != null && !s1.isEmpty()) {
                serverIp = s1 ;
            }
            if (id.isEmpty() || command.isEmpty()){
                resultTextArea.setText("终端ID或者操作命令不能为空！！");
                return ;
            }
            for (int i = 0; i < id.length(); i++){
                if (!Character.isDigit(id.charAt(i))){
                    resultTextArea.setText("终端ID只能包含数字！！");
                    return ;
                }
            }
            for (int j = 0; j < command.length(); j++){
                if (!(Character.isDigit(command.charAt(j))
                    || Character.isLetter(command.charAt(j))
                )){
                    resultTextArea.setText("操作指令只能包含数字和字母！！");
                    return ;
                }
            }
            requestEntity.setEndPointId(id);
            requestEntity.setCommand(command);
            requestEntity.setServerIp(serverIp);
            requestEntity.setTimeOut(timeOut);
            requestEntity.setTcp_utils(tcp);
            if (crcType != null && crcType.equals("小端")){
                requestEntity.setCrcType("small");
            } else if (crcType != null && crcType.equals("大端")) {
                requestEntity.setCrcType("big");
            }
            totalTextField.setText(requestEntity.getTotalCommand());
            try {
                String result = requestEntity.getResult();
                if (result == null || result.isEmpty()){
                    resultTextArea.setText("无数据返回！！");
                    return;
                }
                if (result.equals("error")){
                    resultTextArea.setText("网关故障或者失联！！");
                    return;
                }
                resultTextArea.setText(result);
            } catch (Exception ex) {
                resultTextArea.setText("网络请求失败！！");
            }
        });

        button1.addActionListener(e -> {
            totalTextField.setText("");
            resultTextArea.setText("");
        });
        setLocation(500,200);
        setResizable(false); // 设置窗体为不可调整大小
    }

    public static void main(String[] args) {
        MainWindow window = new MainWindow();
        window.setVisible(true);
    }
}